package com.fenghuajueli.libbasecoreui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;

import java.util.Arrays;
import java.util.List;

public abstract class CommonBaseAdapter<T> extends BaseAdapter {

    protected List<T> data;
    protected Context mContext;
    private int itemWidth, itemHeight;
    public OnListClickListener<T> onListClickListener;


    public CommonBaseAdapter(Context context, List<T> data) {
        this.data = data;
        mContext = context;
    }


    public CommonBaseAdapter(Context context, T[] dataArray) {
        this.data = Arrays.asList(dataArray);
        mContext = context;
    }


    public CommonBaseAdapter(Context context) {
        mContext = context;
    }

    public void setOnListClickListener(OnListClickListener<T> onListClickListener) {
        this.onListClickListener = onListClickListener;
    }

    /**
     * 单个布局的id
     *
     * @return
     */
    public abstract int getLayoutId();


    public int getItemWidth() {
        return itemWidth;
    }

    public void setItemWidth(int itemWidth) {
        this.itemWidth = itemWidth;
    }

    public int getItemHeight() {
        return itemHeight;
    }

    public void setItemHeight(int itemHeight) {
        this.itemHeight = itemHeight;
    }

    /*设置数据源*/
    public void setData(List<T> mListData) {
        this.data = mListData;
        notifyDataSetChanged();
    }

    public List<T> getData() {
        return data;
    }

    /*在列表最后位置添加一条数据*/
    public void addData(T t) {
        if (data != null) {
            data.add(t);
        }
        notifyDataSetChanged();
    }

    /*在指定位置添加一条数据*/
    public void addData(int postition, T t) {
        if (data != null) {
            data.add(postition, t);
        }
        notifyDataSetChanged();
    }

    /*更新数据*/
    public void upData(List<T> mListData) {
        if (mListData != null) {
            mListData.clear();
        }
        setData(mListData);
    }

    /*移除某条数据*/
    public void removeData(T t) {
        if (data != null) {
            data.remove(t);
        }
        notifyDataSetChanged();
    }

    /*移除指定位置的数据*/
    public void removeData(int position) {
        if (data != null) {
            data.remove(position);
        }
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public T getItem(int position) {
        return data == null ? null : data.get(position);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        CommonBaseViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(getLayoutId(), parent,false);
            viewHolder = new CommonBaseViewHolder(view, mContext);
            view.setTag(viewHolder);
        } else {
            viewHolder = (CommonBaseViewHolder) view.getTag();
        }
        if (onListClickListener != null) {
            view.setClickable(true);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onListClickListener.itemClick(position, data.get(position));
                }
            });
        }
        convert(viewHolder, data.get(position), position);
        return view;
    }

    protected abstract void convert(CommonBaseViewHolder baseViewHolder, T data, int position);


}
